// uniCloud-aliyun/category/index.js
'use strict';

const db = uniCloud.database();
const categoryCollection = db.collection('category');

exports.main = async (event, context) => {
    const { action } = event;

    if (action === 'getCategories') {
        try {
            const res = await categoryCollection.orderBy('name', 'asc').get();
            return {
                code: 200,
                msg: '获取成功',
                data: res.data.map(cat => cat.name)
            };
        } catch (error) {
            console.error('获取分类失败:', error);
            return {
                code: -1,
                msg: '服务器错误'
            };
        }
    } else {
        return {
            code: -2,
            msg: '未知操作'
        };
    }
};
